package io.renren.modules.shop.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.shop.entity.CustomerEntity;
import io.renren.modules.shop.entity.ProductEntity;
import io.renren.modules.shop.entity.ShoppingCarsEntity;
import io.renren.modules.shop.service.ProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.shop.entity.CarEntity;
import io.renren.modules.shop.service.CarService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author Yangqianyong
 * @email sunlightcs@gmail.com
 * @date 2021-11-17 15:35:51
 */
@RestController
@RequestMapping("shop/car")
public class CarController {
    @Autowired
    private CarService carService;
    @Autowired
    private ProductService productService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("shop:car:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = carService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("shop:car:info")
    public R info(@PathVariable("id") Long id){
		CarEntity car = carService.getById(id);

        return R.ok().put("car", car);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("shop:car:save")
    public R save(@RequestBody CarEntity car){
		carService.save(car);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("shop:car:update")
    public R update(@RequestBody CarEntity car){
		carService.updateById(car);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("shop:car:delete")
    public R delete(@RequestBody Long[] ids){
		carService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    //    获取购物车	getShoppingCarts:接收用户id     返回该用户的购物车的所有商品
    @RequestMapping("/detail")
    public R getShoppingCarts(@RequestParam Long id){
        List<ShoppingCarsEntity> list = carService.getShoppingCarts(id);

        return R.ok().put("list",list);
    }

    //
//    添加到购物车	addShoppingCart ：接收 用户id 商品id返回成功码 : 0 新加入购物车成功;
//    1 该商品已经在购物车，数量+1 ;2 商品数量达到限购数量 （购物车中该商品数量=库存量
    @RequestMapping("/addCar")
    public R addShoppingCart(@RequestParam Long uid,@RequestParam Long pid){

        QueryWrapper<CarEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CarEntity::getPid,pid);
        queryWrapper.lambda().eq(CarEntity::getUid,uid);
        CarEntity carEntity = carService.getOne(queryWrapper);
        ProductEntity productEntity = productService.getById(pid);
        if(carEntity == null){
            CarEntity carEntity1 = new CarEntity();
            carEntity1.setUid(uid);
            carEntity1.setPid(pid);
            carEntity1.setNum(1);
            carService.save(carEntity1);
            ShoppingCarsEntity shoppingCars = carService.getShoppingCart(uid,pid);
            return R.ok().put("list",shoppingCars);
        }else if(carEntity.getNum()< productEntity.getProductStock()){
            Integer flag = carEntity.getNum();
            carEntity.setNum(flag+1);
            carService.saveOrUpdate(carEntity);
            return R.ok(1);
        }else {
            return R.ok(2);
        }
    }

    //    删除购物车	deleteShoppingCart：接收 用户id 商品id返回成功码
    @RequestMapping("/deleteCar")
    public R deleteShoppingCart(@RequestParam Long uid,@RequestParam Long pid){
        QueryWrapper<CarEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CarEntity::getUid,uid);
        queryWrapper.lambda().eq(CarEntity::getPid,pid);
        carService.remove(queryWrapper);
        return R.ok();
    }
//    updateShoppingCart  接收数据：uid 用户id pid 商品id num 商品数据
    @RequestMapping("/updateNum")
    public R updateShoppingCart(@RequestParam Long uid,@RequestParam Long pid,
                                @RequestParam Integer num){
        QueryWrapper<CarEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CarEntity::getUid,uid);
        queryWrapper.lambda().eq(CarEntity::getPid,pid);
        CarEntity one = carService.getOne(queryWrapper);
        one.setNum(num);
        carService.saveOrUpdate(one);
        return R.ok();
    }

}
